package com.icss.lesson4;

public class TestMain4 {

    public static void main(String[] args) {

        Repo repo = new Repo();
        new Thread("productoer") {
            @Override
            public void run() {
                for(;;) {
                    repo.add();
                }
            }
        }.start();

        new Thread("comsumer") {
            @Override
            public void run() {
                for(;;) {
                    repo.substrcat();
                }
            }
        }.start();

    }
}

class Repo {
    int count;
    public synchronized void add() {
        if (count == 100) {
            try {
                // 释放锁
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notifyAll();
        System.out.println(Thread.currentThread() + "@" + ++count);
    }
    public synchronized void substrcat() {
        if (count == 0) {
            try {
                // 释放锁
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notifyAll();
        System.out.println(Thread.currentThread() + "@" + --count);
    }
}
